package cn.org.itool.ws.framework.thread;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.org.itool.ws.framework.config.NettyProperties;

import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 处理业务的线程池
 */
public class ServiceThreadPool {

    private static volatile ServiceThreadPool instance;

    public static ServiceThreadPool getInstance(NettyProperties nettyProperties){
        if(Objects.isNull(instance)){
            synchronized (ServiceThreadPool.class) {
                if (Objects.isNull(instance)) {
                    instance = new ServiceThreadPool(nettyProperties);
                }
            }
        }
        return instance;
    }
    private  final ExecutorService executor;

    private ServiceThreadPool(NettyProperties nettyProperties) {
        executor = new ThreadPoolExecutor(nettyProperties.getServiceCoreThreads(),
                64, 60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(nettyProperties.getServiceQueueCapacity()),
                new ThreadFactoryBuilder().setNamePrefix("service-pool-").setDaemon(false).build());
    }


    public ExecutorService getExecutor() {
        return executor;
    }
}
